package com.qfedu.edu.iotserver.listener;

import com.qfedu.edu.iotserver.service.ExceptionBusinessService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @author xiaobobo
 * @title: ExceptionMessageReceiveListener
 * @projectName cd-fy-2401-third-project-parent
 * @description: 异常业务消息的接受
 * @date 2024/10/10  9:08
 */
@Component
public class ExceptionMessageReceiveListener {


    //增删改是需要事务的加持的 这个事务 只能是在  业务逻辑类中....
    @Autowired
    private ExceptionBusinessService exceptionBusinessService;

    /**
     * 这个消息实际上是 消费队列中的消息成功 但是业务执行失败的这个消息
     *
     * @param data
     * @param message
     * @param channel
     */
    @RabbitListener(queues = "emqxQueue")
    public void receive(byte[] data, Message message, Channel channel) throws Exception {
        //拿到队列中的这个消息
        String val = new String(data, "UTF-8");
        //接下来这个要执行业务将消息放到 数据库中去
        exceptionBusinessService.handlerExceptionBusinessInfo(val);
        //手动的进行消息的确认 让队列将消息删除
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }

}
